<?php

declare(strict_types=1);
/**
 *  -----------------------------------------------------
 *  Created by : tool
 *  -----------------------------------------------------
 *
 *  -----------------------------------------------------
 *
 *  -----------------------------------------------------
 *  User: tf <1096392101@qq.com>
 *  -----------------------------------------------------
 */
namespace Gao\tool\helper;

class Client
{
    /**
     * 获取客户端ip.
     */
    public static function get_client_ip()
    {
        $ip = $_SERVER['REMOTE_ADDR'] ?? '127.0.0.1';
        if (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']) and preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
            foreach ($matches[0] as $xip) {
                if (! preg_match('#^(10|172\.16|192\.168)\.#', $xip)) {
                    $ip = $xip;
                    break;
                }
            }
        }
        return $ip;
    }
}
